/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package at.irian.web.jsf.orchestra;

import org.apache.myfaces.orchestra.conversation.ConversationContext;
import org.apache.myfaces.orchestra.conversation.ConversationManager;
import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
import org.apache.myfaces.orchestra.requestParameterProvider.RequestParameterProvider;

import java.util.UUID;

/**
 * @author Manfred Geiler
 * @author Gerhard Petracek
 */
class TransactionTokenParameterProvider implements RequestParameterProvider
{
    public static final String TRANSACTION_TOKEN_REQUEST_PARAMETER = "transactionToken";
    public static final String[] REQUEST_PARAMETERS = {TRANSACTION_TOKEN_REQUEST_PARAMETER};

    public String getFieldValue(String field)
    {
        String token = (String) FrameworkAdapter.getCurrentInstance().getRequestAttribute(TransactionTokenParameterProvider.class.getName());

        if (token == null)
        {
            ConversationContext conversationContext = ConversationManager.getInstance().getCurrentConversationContext();

            if (conversationContext != null)
            {
                token = UUID.randomUUID().toString();
                TransactionTokenContainer transactionTokenContainer = getTransactionTokenContainer(conversationContext);
                transactionTokenContainer.setToken(token);

                FrameworkAdapter.getCurrentInstance().setRequestAttribute(TransactionTokenParameterProvider.class.getName(), token);
            }
        }
        return token;
    }

    private TransactionTokenContainer getTransactionTokenContainer(ConversationContext context)
    {
        TransactionTokenContainer container;
        if (context.hasAttribute(TransactionTokenParameterProvider.class.getName()))
        {
            container = (TransactionTokenContainer)context.getAttribute(TransactionTokenParameterProvider.class.getName());
        }
        else
        {
            container = new TransactionTokenContainer();
            context.setAttribute(TransactionTokenParameterProvider.class.getName(), container);
        }
        return container;
    }

    public String[] getFields()
    {
        return REQUEST_PARAMETERS;
    }
}
